package com.diandian.backend.eleme.retail.exchange.service;

import com.diandian.backend.eleme.retail.exchange.req.GetSkuStockReq;
import com.diandian.backend.eleme.retail.exchange.resp.BusinessGetSkuStockResp;
import com.diandian.eleme.retail.boot.starter.dto.SkuListDTO;
import com.diandian.eleme.retail.boot.starter.response.ElemaResponseSkuList;
import com.diandian.eleme.retail.boot.starter.response.ElemaResponseSkuListDataList;
import com.diandian.eleme.retail.boot.starter.service.ElemaSkuManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BusinessSkuManageService {

    @Autowired
    private ElemaSkuManageService skuManageService;

    public BusinessGetSkuStockResp getSkuStock(String source, String secret, GetSkuStockReq data){

        data.check();
        String offlineStoreCode = data.getOfflineStoreCode();
        String skuCode = data.getSkuCode();

        // 构建参数
        SkuListDTO dataDTO = new SkuListDTO();
        dataDTO.setPage(1);
        dataDTO.setPagesize(1);
        dataDTO.setCustom_sku_id(data.getSkuCode());
        dataDTO.setShop_id(offlineStoreCode);

        // 执行查询
        ElemaResponseSkuList response = null;
        try {
            response = skuManageService.skuList(source, secret, dataDTO);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        if(response == null) {
            BusinessGetSkuStockResp item = new BusinessGetSkuStockResp();
            item.setOnlineStoreCode(null);
            item.setOfflineStoreCode(offlineStoreCode);
            item.setSkuCode(skuCode);
            item.setCurQty(null);
            item.setInQty(null);
            item.setPreQty(null);
            item.setLockQty(null);
            item.setLastModificationTime(null);
            item.setMessage("查询异常");
            return item;
        } else if(response.getErrno() == null || "".equals(response.getErrno()) || response.getData() == null
                || response.getData().getList() == null || response.getData().getList().size() == 0){
            BusinessGetSkuStockResp item = new BusinessGetSkuStockResp();
            item.setOnlineStoreCode(null);
            item.setOfflineStoreCode(offlineStoreCode);
            item.setSkuCode(skuCode);
            item.setCurQty(null);
            item.setInQty(null);
            item.setPreQty(null);
            item.setLockQty(null);
            item.setLastModificationTime(null);
            item.setMessage("未查询到库存数据");
            return item;
        } else {
            ElemaResponseSkuListDataList dataItem = response.getData().getList().get(0);
            Integer stockCount = dataItem.getLeft_num();

            BusinessGetSkuStockResp item = new BusinessGetSkuStockResp();
            item.setOnlineStoreCode(null);
            item.setOfflineStoreCode(offlineStoreCode);
            item.setSkuCode(skuCode);
            item.setCurQty(null);
            item.setInQty(stockCount == null ? 0 : stockCount.longValue());
            item.setPreQty(null);
            item.setLockQty(null);
            item.setLastModificationTime(new Date());
            item.setMessage(null);
            return item;
        }
    }
}
